package com.manager.system.controller;

import com.manager.common.annotation.AspectLogInfo;
import com.manager.common.constant.AspectMethodOperationEnum;
import com.manager.common.constant.OperationLogEnumType;
import com.manager.common.utils.ObjectUtils;
import com.manager.common.vo.PageVO;
import com.manager.common.vo.ResultVO;
import com.manager.model.System_Role;
import com.manager.system.service.RoleService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.CycleDetectionStrategy;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @RequestMapping("/getRolePage")
    public PageVO getRolePage(System_Role model, HttpServletRequest request, HttpServletResponse response) throws IOException {
        PageVO pageVO  = roleService.getRoleList(model);
        return pageVO;
    }

    @RequestMapping("/getRoleTreeData")
    public ResultVO getRoleTreeData(System_Role model, HttpServletRequest request, HttpServletResponse response) throws IOException{
        try{
            JSONArray jsonArray = roleService.getRoleTreeData();
            return ResultVO.success(jsonArray);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultVO.failed();
    }

    @RequestMapping("/addOrUpdate")
    @ResponseBody
    @AspectLogInfo(type= AspectMethodOperationEnum.SAVE,tableName="tb_sys_role", content="添加或者修改角色信息",
            logType = OperationLogEnumType.ROLE,assist = true,claz = System_Role.class)
    public ResultVO addOrUpdate(System_Role model,HttpServletRequest request,HttpServletResponse response) throws IOException{
        try{
            if(StringUtils.isEmpty(model.getId())){
                roleService.addRole(model);
            }else{
                roleService.updateRole(model);
            }
            return ResultVO.success();
        }catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return ResultVO.failed();
    }

    @RequestMapping("/getMenuTree")
    public ResultVO getMenuTree(System_Role model,HttpServletRequest request,HttpServletResponse response) throws IOException{
        try {
            JSONArray jsonArray = roleService.getMenuTree(model);
            return ResultVO.success(jsonArray);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultVO.failed();
    }

    @RequestMapping("/getUserMenuByRoleId")
    public ResultVO getUserRoleInfo(System_Role model,HttpServletRequest request,HttpServletResponse response) throws IOException{
        try {
            JSONArray jsonArray = roleService.getUserMenuByRoleId(model.getId());//测试用
            return ResultVO.success(jsonArray);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultVO.failed();
    }

    @RequestMapping("/deleteRole")
    @AspectLogInfo(type= AspectMethodOperationEnum.DELETE,tableName="tb_sys_role", content="删除角色信息", logType = OperationLogEnumType.ROLE)
    public ResultVO deleteRole(HttpServletResponse response,System_Role model) throws IOException{
        try{
            roleService.deleteRole(model);
            return ResultVO.success();
        }catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return ResultVO.failed();
    }

    @RequestMapping("/getSystemRoleList")
    @ResponseBody
    public ResultVO getSystemRoleList(System_Role model,HttpServletRequest request,HttpServletResponse response) throws Exception{
       try{
           List<System_Role> list = roleService.getSystemRoleList();
           return ResultVO.success(ObjectUtils.getJSONArray(System_Role.config,list));
       }catch (Exception e){
           e.printStackTrace();
       }
       return ResultVO.failed();
    }
}
